package githubleetcode

import java.util.*


fun main() {

    //[ 0 , 30] ,[ 5 , 10 ] ， [15 , 20 ]
    val interOverlap = interOverlap(arrayOf(arrayOf(5, 10), arrayOf(0, 30), arrayOf(15, 20)))

    println(interOverlap)

//    var pq = PriorityQueue<Int>()
//
//    pq.offer(0)
//
//    println(pq.peek())
//
//    pq.offer(50)
//
//    println(pq.peek())
//
//    pq.offer(-2)
//
//    println(pq.peek())
}


fun interOverlap(times: Array<Array<Int>>): Int {

    times.sortBy { it[0] }

    val pq = PriorityQueue<Int>(times.size)

    var max = 0

    for (i in 0 until times.size) {

        val time = times[i]

        //移除所有结束时间 超过当前女孩进入时间 的女生
        while (pq.isNotEmpty() && time[0] > pq.peek()) {
            pq.poll()
        }

        pq.offer(time[1])

        max = Math.max(max, pq.size)
    }

    return max
}